package com.lhj.xxsc.order.controller;

import com.lhj.xxsc.order.entity.OrderEntity;
import com.lhj.xxsc.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @author 罗惠江
 * @version 1.0
 * @email 1511619294@qq.com
 * @date 2021/1/3 17:43
 */
@Slf4j
@RestController
public class RabbitController {
	@Autowired
	RabbitTemplate rabbitTemplate;

	@GetMapping("/sendMq")
	public String sendMq(@RequestParam(value = "num", defaultValue = "10") Integer num) {
		for (int i = 0; i < num; i++) {
			if (i % 2 == 0) {
				OrderEntity orderEntity = new OrderEntity();
				orderEntity.setId(1L);
				orderEntity.setReceiverName("测试数据");
				//  CorrelationData 自定义唯一id 我们可以在发消息的时候存一个
				rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", orderEntity, new CorrelationData(UUID.randomUUID().toString()));
				log.info("消息发送成功");
			} else {
				OrderReturnReasonEntity orderReturnReasonEntity = new OrderReturnReasonEntity();
				orderReturnReasonEntity.setId(1L);
				orderReturnReasonEntity.setName("测试数据2");
				rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", orderReturnReasonEntity, new CorrelationData(UUID.randomUUID().toString()));
				log.info("消息发送成功");
			}
		}
		return "ok";
	}
}
